<?php
declare (strict_types=1);

namespace app\content\logic\content;

use app\content\model\content\Agreement as AgreementModel;
use think\facade\Request;
use think\Model;

/**
 * 协议逻辑层
 */
class Agreement extends AgreementModel
{
    /**
     * 协议页面
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-06-11
     */
    public function fetch($key)
    {
        $field      = 'id,key,title,remark,update_time';
        $deviceType = request()->device_type;
        $showCont   = in_array($deviceType, ['h5', 'pc', 'miniprogram']);
        // if (request()->is_pc) $field .= ',content';
        if ($showCont) $field .= ',content';
        $mhm_id = $key == 'schoolUser' ? 1 : request()->mhm_id; // , 'mobileVip'
        $data   = $this->where('mhm_id', $mhm_id)->where('key', $key)->field($field)->findOrFail();
        if (!$showCont) $data->content = Request::domain() . '/content/agreement/' . $key . '/content/' . $mhm_id;
        // if (!request()->is_pc) $data->content = Request::domain() . '/content/agreement/' . $key . '/content/' . $mhm_id;

        return $data;
    }

    /**
     * 协议内容
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-06-11
     */
    public function content($key, $mhm_id)
    {
        return $this->where('mhm_id', $mhm_id)->where('key', $key)->value('content');
    }

    /**
     * 根据条件获取信息
     *
     * @param array $where
     * @return Agreement|array|Model
     */
    public function getInfoByWhere(array $where = [])
    {
        return $this->where($where)->findOrEmpty();
    }
}
